package com.donger.business.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.upms.entity.SysApp;
import com.donger.business.upms.service.ISysAppService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.data.entity.DropDown;
import com.donger.common.query.enums.CommonQuery;
import com.donger.common.query.utils.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys/app")
@Api(tags = "应用管理模块")
public class SysAppController {


    private final ISysAppService sysAppService;

    @GetMapping("/page")
    public Result page(Page<SysApp> page, CommonQuery commonQuery, SysApp app) {
        QueryWrapper<SysApp> wrapper = QueryGenerator.initQueryWrapper(app, commonQuery);
        sysAppService.page(page, wrapper);
        return Res.ok(page);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增应用")
    public Result save(@RequestBody SysApp entity) {
        sysAppService.save(entity);
        return Res.ok(entity);
    }

    @PostMapping("/update")
    @ApiOperation(value = "新增应用")
    public Result update(@RequestBody SysApp entity) {
        sysAppService.updateById(entity);
        return Res.ok(entity);
    }

    @GetMapping("/info")
    public Result info(Long id) {
        SysApp sysApp = sysAppService.getById(id);
        return Res.ok(sysApp);
    }


    @DeleteMapping("/delete")
    public Result delete(Long id) {
        sysAppService.removeById(id);
        return Res.ok();
    }


    @PostMapping("/setAsDefault")
    @ApiOperation(value = "系统应用_设为默认应用")
    public Result setAsDefault(@RequestBody SysApp entity) {

        sysAppService.setAsDefault(entity);
        return Res.ok();
    }

    /**
     * 应用下拉列表
     *
     * @return
     */
    @GetMapping("/dropdown")
    @ApiOperation(value = "系统应用_下拉列表")
    public Result dropdown() {
        List<DropDown> dropDowns = sysAppService.list().stream().map(item -> {
            DropDown dropDown = new DropDown();
            dropDown.setLabel(item.getName());
            dropDown.setValue(item.getCode());
            return dropDown;
        }).collect(Collectors.toList());
        return Res.ok(dropDowns);
    }


}
